package com.ruoyi.project.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.domain.dto.DayangDTO;
import com.ruoyi.project.mapper.DayangMapper;
import com.ruoyi.project.domain.entity.Dayang;
import com.ruoyi.project.service.DayangService;
import org.springframework.stereotype.Service;

import lombok.AllArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;

/**
 * 打样费用(Dayang)表服务实现类
 *
 * @author yanweichao
 * @since 2025-06-22 11:26:36
 */
@Service
@AllArgsConstructor
public class DayangServiceImpl extends ServiceImpl<DayangMapper, Dayang> implements DayangService {

    protected LambdaQueryWrapper<Dayang> queryWrapper(DayangDTO param) {
        return new LambdaQueryWrapper<Dayang>()
                .like(StrUtil.isNotEmpty(param.getKeyword()), Dayang::getDydh, param.getKeyword())
                .orderByDesc(Dayang::getId);
    }

    @Override
    public Page<?> listPage(Page<Dayang> page, DayangDTO param) {
        return this.page(page, queryWrapper(param));
    }

    @Override
    public List<Dayang> queryList(DayangDTO param) {
        return this.list(queryWrapper(param));
    }

    @Override
    public String genNo() {
        String prefix = "Y" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd"));
        String no = Optional.ofNullable(this.getBaseMapper().getMaxNo(prefix)).orElse("0");
        return prefix + StrUtil.fillBefore(String.valueOf(Integer.parseInt(no) + 1), '0', 3);
    }

    @Override
    public boolean saveOrUpdate(DayangDTO param) {
        return param.isNew() ? this.save(param) : this.updateById(param);
    }
}
